package com.czl.module_storehouse.activity.borrow.details;

import android.annotation.SuppressLint;
import android.os.Bundle;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.czl.base.data.bean.tengyun.BorrowBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.base.BaseDetailsActivity;
import com.czl.module_storehouse.adapter.ReceiveDetailsAdapter;
import com.czl.module_storehouse.databinding.HeaderBorrowDetailsBinding;
import com.czl.module_storehouse.event.BorrowEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;


/**
 * @author xubin
 */
public class BorrowDetailsActivity extends BaseDetailsActivity {

    private HeaderBorrowDetailsBinding mHeaderBinding;
    private ReceiveDetailsAdapter mAdapter;

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("借用详情");
        EventBus.getDefault().register(this);
    }

    @Override
    protected BaseQuickAdapter<?, ?> getAdapter() {
        if (mAdapter == null) {
            mAdapter = new ReceiveDetailsAdapter(R.layout.item_receive_details, "已借：");
            mHeaderBinding = HeaderBorrowDetailsBinding.inflate(getLayoutInflater());
            mAdapter.addHeaderView(mHeaderBinding.getRoot());
        }
        return mAdapter;
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(BorrowEvent event) {
        BorrowBean borrowBean = event.getBorrowBean();
        if (borrowBean != null) {
            initHeaderView(borrowBean);
            mFooterBinding.etRemark.setText(borrowBean.getBorrowComment());
            List<SortBean> sortList = borrowBean.getSortList();
            if (sortList != null) {
                mAdapter.setList(sortList);
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private void initHeaderView(BorrowBean borrowBean) {
        mHeaderBinding.clHeaderInfo.ivHeader.setImageResource(R.mipmap.icon_lingyong_head);
        if (borrowBean == null) {
            return;
        }
        mHeaderBinding.clHeaderInfo.tvHeaderTitle.setText(borrowBean.getBorrowName());
        mHeaderBinding.clHeaderInfo.tvHeaderDate.setText(borrowBean.getBorrowDate());
        mHeaderBinding.clHeaderInfo.tvHeaderCode.setText("借用单：" + (borrowBean.getBorrowCode() == null ? "" : borrowBean.getBorrowCode()));
        mHeaderBinding.tvHeaderDepartment.setText("借用部门：" + (borrowBean.getBorrowPersonDepartment() == null ? "" : borrowBean.getBorrowPersonDepartment()));
        mHeaderBinding.tvHeaderDesc.setText("借用原因：" + (borrowBean.getApplyComment() == null ? "" : borrowBean.getApplyComment()));
        mHeaderBinding.tvHeaderCompany.setText("借用公司：" + (borrowBean.getBorrowCompanyName() == null ? "" : borrowBean.getBorrowCompanyName()));
        mHeaderBinding.tvReturnDate.setText("拟还日期：" + (borrowBean.getMoReturnDate() == null ? "" : borrowBean.getMoReturnDate()));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHeaderBinding != null) {
            mHeaderBinding.unbind();
        }
    }
}
